/* 
 * File:   PingXpackDetector.cpp
 * Author: minaev
 * 
 * Created on 23 Декабрь 2013 г., 13:43
 */

#include "PingXpackDetector.h"

PingXpackDetector::PingXpackDetector(XbeeContainer* xcont) : XpackDetector(xcont) {
}

uint8_t PingXpackDetector::getXpackType() {
    return XAPI_RC_RESP;
}

bool PingXpackDetector::isXpackValid(Xpack* pack) {
    return (pack->getByte(12) == 'S' && pack->getByte(13) == 'L');
}

void PingXpackDetector::action(Xpack* pack) {
    uint32_t buf;
    int nblock = xcont->findById(*(uint32_t*) (pack->getData() + 6));
    if (nblock == -1) {
        if (*(uint32_t*) (pack->getData() + 6) != 0) {
            Xmsg::printf("registration: 0x%x\n", *(uint32_t*) (pack->getData() + 6));
            nblock = xcont->addXbee(*(uint32_t*) (pack->getData() + 6));
        }
    } else {
        buf = *(uint8_t*) xcont->getParam(nblock, SLN_XPINGS) + 1;
        xcont->setParam(nblock, SLN_XPINGS, &buf);

        if (*(uint8_t*) xcont->getParam(nblock, SLN_XBTNS) !=
                *(uint8_t*) xcont->getParam(nblock, SLN_XBTNOLDS)) {
            if ((*(uint8_t*) xcont->getParam(nblock, SLN_XBTNS)) == 1) {
                xcont->pinAlarmOn(nblock);
            } else {
                xcont->pinAlarmOff(nblock);
            }
            xcont->setParam(nblock, SLN_XBTNOLDS,
                    xcont->getParam(nblock, SLN_XBTNS));
        }
    }
}